
var Form = {
	formId:'',
	formType:'',
	fieldNamePre:'',
	fields:null,
	dicsMap:null,
	buttons:null,
	resObj:null,
	data:null,
	tabPage:null,
	
	init:function(){
		if(!Form.formId||!Form.fields||!Form.formType){
			return;
		}
		if(Form.formType=='search'){
			Form.initSearchForm();
		}else if(Form.formType=='ins'||Form.formType=='upd'){
			Form.initForm();
		}
		Form.initButton();
		Form.initData();
	},
	initSearchForm:function(){
		var fieldsHtml = "";
		$.each(Form.fields,function(i,field){
			var inputHtml = Form.parseFieldHtml(field);
			if(i%2==0){
				fieldsHtml += "<tr>";
			}
			fieldsHtml += "<td>"+field.tagTitle+"：</td>";
			fieldsHtml += "<td>"+inputHtml+"</td>";
			if(i%2==1||i==(Form.fields.length -1)){
				fieldsHtml += "</tr>";
			}
		});
		$("#"+Form.formId).find('table').append(fieldsHtml);
	},
	initForm:function(){
		Form.toMapField();
		if(Form.tabPage&&Form.tabPage.length>0){
			Form.createTabForm();
		}else{
			var tableObject = $("#"+Form.formId).find('table');
			var formTable = "";
			if(ToolsUtil.isNotNull(Form.resObj.arrayFmt)){
				formTable = Form.createFmtFormTable(fmtField);
			}else{
				formTable = Form.createFormTable();
			}
			$(tableObject).append(formTable);
			Form.validateForm();
		}
	},
	createFormTable:function(){
		var formTable = "";
		$.each(Form.fieldMap,function(fieldName,field){
			var inputHtml = Form.parseFieldHtml(field);
			formTable += "<tr>";
			formTable += "<td class='formTdText'>"+field.tagTitle+"：</td>";
			formTable += "<td class='formTdInput'>"+inputHtml+"</td>";
			formTable += "</tr>";
		});
		return formTable;
	},
	/*
	showField:function(field,object){
		if(field.tagType==4){//隐藏域
			Form.dealHidenField(field);
		}else{
			var inputHtml = Form.parseFieldHtml(field);
			var fieldsHtml = "";
			fieldsHtml += "<tr>";
			fieldsHtml += "<td class='formTdText'>"+field.tagTitle+"：</td>";
			fieldsHtml += "<td class='formTdInput'>"+inputHtml+"</td>";
			fieldsHtml += "</tr>";
			$(object).append(fieldsHtml);
			Form.validate(field);
		}
	},
	*/
	createTabForm:function(){
		var tabsHtml = "<div id='tabs'></div>";
		$("#"+Form.formId).html(tabsHtml);
		$('#tabs').tabs({    
		    border:false
		});
		$.each(Form.tabPage,function(i,tab){
			var tabOption = {};
			tabOption.title = tab.pageTitle;
			var fmtField = tab.arrayFmt;
			var formTable = Form.createFmtFormTable(fmtField);
			tabOption.content = '<table class="formTable" width="100%" id="tab_'+i+'">'+formTable+'</table>';
			$('#tabs').tabs('add',tabOption);
			/*
			var fieldNames = fmtField.split(/,|;/);
			var tableObject = $("#"+Form.formId).find('table#tab_'+i);
			$.each(fieldNames,function(i,fieldName){
				fieldName = fieldName.replace(/ /g,"");
				if(fieldName==""){
					System.showError('错误', "必须提供排列格式或数据表格!");
					return;
				}
				var field = Form.fieldMap[fieldName];
				Form.showField(field,tableObject);
			});
			*/
		});
		Form.validateForm();
		$('#tabs').tabs('select',0);
	},
	createFmtFormTable:function(arrayFmt){
		var fieldRows = arrayFmt.split(";");
		var maxCol = 0;
		for(var i=0;i<fieldRows.length;i++){
			var fieldRow = fieldRows[i];
			fieldRow = fieldRow.replace(/ /g,"");
			if(fieldRow!=""){
				var fieldCols = fieldRow.split(",");
				for(var j=0;j<fieldCols.length;j++){
					var fieldCol = fieldCols[j];
					fieldCols[j] = fieldCol;
				}
				if(fieldCols.length>maxCol){
					maxCol = fieldCols.length;
				}
				fieldRows[i] = fieldCols;
			}
		}
		
		var formTable = "";
		for(var i=0;i<fieldRows.length;i++){
			var fieldCols = fieldRows[i];
			formTable += "<tr>";
			var m = 0;
			for(var j=0;j<fieldCols.length;j++){
				var fieldName = fieldCols[j];
				var field = Form.fieldMap[fieldName];
				var inputHtml = Form.parseFieldHtml(field);
				var colspan = 1;
				if (j == fieldCols.length - 1){
					colspan = maxCol * 2 - m - 1;
				}
				var colspanHtml = "";
				if (colspan > 1){
					colspanHtml = " colspan='" + colspan + "'";
				}
				formTable += "<td class='formTdText'>"+field.tagTitle+"：</td>";
				formTable += "<td class='formTdInput' "+colspanHtml+">"+inputHtml+"</td>";
				m += 1 + colspan;
			}
			formTable += "</tr>";
		}
		return formTable;
	},
	toMapField:function(){
		var fieldMap = {};
		$.each(Form.fields,function(i,field){
			var tagName = field.tagName;
			fieldMap[tagName] = field;
		});
		Form.fieldMap = fieldMap;
	},
	parseFieldHtml:function(field){
		var inputStr = "";
		var fieldName = field.tagName;
		var width = 20;
		if(field.tagLen){
			width = field.tagLen;
		}
		var value = "";
		if(Form.data){
			value = Form.data[fieldName];
		}else if(field.initValue){
			value = field.initValue;
		}
		if(!ToolsUtil.isNotNull(value)){
			value = "";
		}
		
		if(field.tagType==7){//单行输入框
			inputStr = "<input type='text' size='"+width+"' name='"+Form.fieldNamePre+fieldName+"' value='"+value+"'/>";
		}else if(field.tagType==8){//多行输入框
			inputStr = "<textarea rows='3' cols='50' name="+Form.fieldNamePre+fieldName+">"+value+"</textarea>";
//			inputStr = "<input class='easyui-textbox' data-options='multiline:true' value='"+value+"' style='width:300px;height:50px'>";
		}else if(field.tagType==11){//可编辑弹出字典
			inputStr = "<input type='text' size='"+width+"' name='"+Form.fieldNamePre+fieldName+"' id='"+Form.fieldNamePre+fieldName+"' value='"+value+"'/>";
			inputStr += "<a href='javascript:void(0)' onclick=\"javascript:System.popSetDic('"+field.dicSubSys+"',"+field.dicKind+","+field.dicGrpid+","+field.valDicCol+",'"+Form.fieldNamePre+fieldName+"');\">选择</a>";
		}else if(field.tagType==5){//密文
			inputStr = "<input type='password' size='"+width+"' name='"+Form.fieldNamePre+fieldName+"' id='"+Form.fieldNamePre+fieldName+"' value='"+value+"'/>";
		}else if(field.tagType==2){//下拉框
			inputStr = "<select id='"+Form.fieldNamePre+fieldName+"' name='"+Form.fieldNamePre+fieldName+"' style='min-width: 120px;'>";
			inputStr += "<option value=''>--请选择--</option>";
			if((field.dicKind==1||field.dicKind==4)&&Form.dicsMap){
				var dicKey = field.dicKind +"_"+field.dicGrpid;
				var dics = Form.dicsMap[dicKey];
				if(dics){
					$.each(dics,function(i,dic){
						var sel = Form.data&&Form.data[fieldName]==dic.key ? 'selected="selected"' : '';
						var dicKey = dic.key;
						if(field.valDicCol==2){
							dicKey = dic.value;
						}
						inputStr += "<option value='"+dicKey+"' "+sel+">"+dic.value+"</option>";
					});
				}
			}
			inputStr += "</select>";
		}else if(field.tagType==-1||field.tagType==-4){
			inputStr = value;
			inputStr += "<input type='hidden' name='"+Form.fieldNamePre+fieldName+"' value='"+value+"'/>";
		}
		if(field.tagType!=-1&&field.postFixTag){
			inputStr = inputStr + field.postFixTag;
		}
		return inputStr;
	},
	/*
	dealHidenField:function(field){
		var value = "";
		if(Form.data){
			value = Form.data[fieldName];
		}else if(field.initValue){
			value = field.initValue;
		}
		var hideInputHtml = "<input type='hidden' name='"+Form.fieldNamePre+field.tagName+"' value='"+value+"'>";
		$("#"+Form.formId).prepend(hideInputHtml);
	},
	*/
	validateForm:function(){
		$.each(Form.fieldMap,function(fieldName,field){
			var validOption = {};
			if(!field.nullAble){
				validOption.required = true;
				validOption.missingMessage = field.tagTitle+'不能为空';
			}
			if(field.tagType==7){
				$("input[name='"+Form.fieldNamePre+field.tagName+"']").validatebox(validOption);
			}else if(field.tagType==8){
				validOption.multiline = true;
				validOption.height = 60;
				$("textarea[name='"+Form.fieldNamePre+field.tagName+"']").textbox(validOption);
			}else if(field.tagType==2){
				if(!field.nullAble){
					validOption.editable = false;
					validOption.validType = "comboxRequired['#"+Form.fieldNamePre+field.tagName+"']";
				}
				$("select[name='"+Form.fieldNamePre+field.tagName+"']").combobox(validOption);
			}
		});
	},
	/*
	validate:function(field){
		var validOption = {};
		if(!field.nullAble){
			validOption.required = true;
			validOption.missingMessage = field.tagTitle+'不能为空';
		}
		if(field.tagType==7){
			$("input[name='"+Form.fieldNamePre+field.tagName+"']").validatebox(validOption);
		}else if(field.tagType==8){
			validOption.multiline = true;
			validOption.height = 60;
			$("textarea[name='"+Form.fieldNamePre+field.tagName+"']").textbox(validOption);
		}else if(field.tagType==2){
			if(!field.nullAble){
				validOption.editable = false;
				validOption.validType = "comboxRequired['#"+Form.fieldNamePre+field.tagName+"']";
			}
			$("select[name='"+Form.fieldNamePre+field.tagName+"']").combobox(validOption);
		}
	},
	*/
	initData:function(){
		if(!Form.data){
			return;
		}
		for(var j in Form.data){
			var value = "";
			if(Form.data[j]!='null'&&Form.data[j]!=null){
				value = Form.data[j];
			}
			var hideInputHtml = "<input type='hidden' name='orig_"+j+"' value='"+value+"'>";
			$("#"+Form.formId).prepend(hideInputHtml);
		}
	},
	getFormData:function(){
		var dataJson = $("#"+Form.formId).serializeJson();
		return dataJson;
	},
	initButton:function(){
		if(Form.formType=='search'){
			Form.initSearchButton();
		}
	},
	initSearchButton:function(){
		var buttonHtml = "<td>";
		buttonHtml += "<a href=\"javascript:void(0)\" id=\"searchButton\" class=\"easyui-linkbutton my-search-button\" iconCls=\"icon-search\" plain=\"true\">查询</a>";
		buttonHtml += "<a href=\"javascript:void(0)\" id=\"resetButton\" class=\"easyui-linkbutton my-search-button\" iconCls=\"icon-reset\" plain=\"true\">重置</a>";
		buttonHtml += "</td>";
		$("#"+Form.formId).find('table tr:last').append(buttonHtml);
		$('#searchButton').linkbutton();
		$('#resetButton').linkbutton();
		$("#searchButton").click(function(){
			if(typeof Form.resObj.search =='function'){
				Form.resObj.search();
			}
//			search();
		});
		$("#resetButton").click(function(){
			$("#"+Form.formId)[0].reset();
		});
	}
}



